package com.itheima.book.dao;

import com.itheima.book.domain.Book;

import java.util.ArrayList;

public class Book2Dao implements BookBaseDao {

    ArrayList<Book> b = new ArrayList<>();

    public void deleteByBookId(String bookId) {

        int a = -1;
        for (int i = 0; i < b.size(); i++) {
            Book book = b.get(i);
            if(book!=null){
                if(book.getBookId().equals(bookId)){
                    a=i;
                    break;
                }
            }
        }

        if(a!=-1){
            b.remove(a);
        }

    }


    public boolean addBook(Book book){
        b.add(book);

        return true;
    }





    public Book[] allBook(){
       Book[] books = new Book[b.size()];

        for (int i = 0; i < b.size(); i++) {
            Book book = b.get(i);
            books[i] = book;
        }
        return books;
    }




    public void  updateBookByBookId(Book book) {
        int a = -1;
        for (int i = 0; i < b.size(); i++) {
            Book book1 = b.get(i);
            if (book1 != null) {
                if (book1.getBookId().equals(book.getBookId())) {
                    a = i;
                    break;
                }
            }
        }

        if (a != -1) {
            b.set(a, book);
        }
    }
}
